"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : show plugins 删除插件检查插件显示
Create At   : 2022.08.17
@zou_jialiang0501129488
Description :
    1、创建兼容B库
    2、设置guc参数support_extended_features=on
    3、删除插件并执行show plugins查询插件状态
    4、恢复环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
    modified by @zou_jialiang050,2023.01.18，修改SQL执行的指定数据库&&调整用例格式
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest02(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.common = Common()
        self.commonsh = CommonSH()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.db_name = "db_show_0062"
        self.parameter = "support_extended_features"

    def test_show(self):
        text = '-----step1:创建和node.yml同名且兼容b库的数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:设置参数为on;expect:成功-----'
        self.log.info(text)
        self.log.info('-----检查数据库状态-----')
        status = self.sh_primary.get_db_cluster_status(param='detail')
        self.log.info(status)
        self.assertIn('Normal', status, '数据库状态异常')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_guc set -N all -I all -c '{self.parameter}=on'"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn(self.constant.GSGUC_SUCCESS_MSG, res, 'guc参数设置失败')
        res = self.sh_primary.restart_db_cluster(get_detail=True)
        self.log.info(res)
        self.assertIn(self.constant.START_SUCCESS_MSG, res, '数据库重启失败')

        text = '-----step3:删除插件并使用show plugins查看插件状态;' \
               'expect:成功-----'
        self.log.info(text)
        sql = f"drop extension hstore;"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=f"{self.db_name}")
        self.log.info(res)
        self.assertIn(self.constant.drop_extension_success, res,
                      '插件删除失败')

        sql = f"show plugins;"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=f"{self.db_name}")
        self.log.info(res)
        check_msg = 'hstore          | DISABLED'
        self.assertIn(check_msg, res, '插件状态检查失败')

    def tearDown(self):
        self.log.info('-----step4:恢复环境;expect:成功-----')
        sql = f"create extension hstore;"
        self.log.info(sql)
        res1 = self.sh_primary.execut_db_sql(sql, dbname=f"{self.db_name}")
        self.log.info(res1)
        sql = f"show plugins;"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=f"{self.db_name}")
        self.log.info(res)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_guc set -N all -I all -c '{self.parameter}=off'"
        self.log.info(cmd)
        res2 = self.common.get_sh_result(self.user_node, cmd)
        self.log.info(res2)
        res3 = self.sh_primary.restart_db_cluster(get_detail=True)
        self.log.info(res3)
        sql_cmd = f"clean connection to all force for " \
                  f"database {self.db_name};" \
                  f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.create_extension_success, res1,
                      '创建插件失败')
        self.assertIn('hstore          | ACTIVE', res, '插件状态检查失败')
        self.assertIn(self.constant.GSGUC_SUCCESS_MSG, res2, 'guc参数设置失败')
        self.assertIn(self.constant.START_SUCCESS_MSG, res3, '数据库重启失败')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '数据库删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end------')
